package com.jason.demo.servicehi;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zhouxinghuai
 * @description description
 * @date 2018-08-09
 */
public class StreamDemo {

    public static void main(String[] args) {
        List<Persons> list1 = getList(10);
        List<Persons> list2 = getList(20);
        List<Persons> list3 = getList(30);

        List<Persons> multistreams =
                Stream.of(list1, list2, list3)
                        .flatMap(childList -> childList.stream()).collect(Collectors.toList());

        List<String> list =
                list1.stream()
                        .filter(p -> p.getName().startsWith("D"))
                        .map(p -> p.getName())
                        .map(String::toLowerCase)
                        .collect(Collectors.toList());

        Stream.of("one", "two", "three", "four", "five")
                .filter(e -> e.length() > 3)
                .peek(e -> System.out.println("Filtered value: " + e))
                .map(String::toUpperCase)
                .peek(e -> System.out.println("Mapped value: " + e))
                .map(String::toLowerCase)
                .peek(e -> System.out.println("Mapped value2: " + e))
                .collect(Collectors.toList());

//        System.out.println(multistreams);


    }


    public static List<Persons> getList(int count) {
        int[] ages = {18, 23, 25, 16, 37, 25, 28, 19, 30, 21, 36, 20, 22, 33};
        String[] names = {"Alan", "Bob", "Chris", "David", "Elisabeth", "Franklin", "Alan", "Bob", "Chris", "David", "Dlisabeth", "Franklin", "Alan", "Bob", "Chris", "David", "Elisabeth", "Dranklin",
                "Alan", "Bob", "Chris", "David", "Elisabeth", "Franklin", "Alan", "Bob", "Chris"};
        String[] jobs = {"doctor", "teacher", "student", "lawer", "engineer", "boss", "worker"};
        Random random = new Random();
        List<Persons> personsList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Persons p = new Persons();
            p.setName(names[random.nextInt(100) % names.length]);
            p.setJob(jobs[random.nextInt(100) % jobs.length]);
            p.setAge(ages[random.nextInt(100) % ages.length]);
            personsList.add(p);
        }
        return personsList;
    }


    /*public static optionalTest() {
        String strA = " abcd ", strB = null;
        print(strA);
        print("");
        print(strB);
        getLength(strA);
        getLength("");
        getLength(strB);
        public static void print(String text) {
            // Java 8
            Optional.ofNullable(text).ifPresent(System.out::println);
            // Pre-Java 8
            if (text != null) {
                System.out.println(text);
            }
        }
        public static int getLength(String text) {
            // Java 8
            return Optional.ofNullable(text).map(String::length).orElse(-1);
            // Pre-Java 8
// return if (text != null) ? text.length() : -1;
        };
    }*/

}
